package jude.java.framework.beans;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang.StringUtils;

public class Regexp {
	private static final int DEFAULT_FLAGS = Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL;

	private int type;
	private String start;
	private String end;
	private String pattern;
	private int flags;
	private int group;
	
	public Regexp() {
	}
	
	public Regexp(String pattern) {
		this(pattern, 1, 0, DEFAULT_FLAGS);
	}
	
	public Regexp(String pattern, int group) {
		this(pattern, group, 0, DEFAULT_FLAGS);
	}
	
	public Regexp(String pattern, int group, int type) {
		this(pattern, group, type, DEFAULT_FLAGS);
	}
	
	public Regexp(String pattern, int group, int type, int flags) {
		this.pattern = pattern;
		this.group = group;
		this.flags = flags;
		this.type = type;
	}
	
	public Regexp(String start, String pattern, String end) {
		this(start, pattern, end, 0, 0, DEFAULT_FLAGS);
	}

	public Regexp(String start, String pattern, String end, int group, int type) {
		this(start, pattern, end, group, type, DEFAULT_FLAGS);
	}
	
	public Regexp(String start, String pattern, String end, int group, int type, int flags) {
		this.start = start;
		this.end = end;
		this.pattern = pattern;
		this.group = group;
		this.flags = flags;
		this.type = type;
		o();
	}
	
	public int getFlags() {
		return flags;
	}

	public void setFlags(int type) {
		this.flags = type;
	}
	
	public int getType() {
		return type;
	}
	
	public void setType(int type) {
		this.type = type;
	}

	public String getStart() {
		return start;
	}

	public void setStart(String start) {
		this.start = start;
	}

	public String getEnd() {
		return end;
	}

	public void setEnd(String end) {
		this.end = end;
	}

	public String getPattern() {
		return pattern;
	}

	public void setPattern(String pattern) {
		this.pattern = pattern;
	}

	public int getGroup() {
		return group;
	}

	public void setGroup(int group) {
		this.group = group;
	}
	
	///////////////////////////////////////////////////////////////////
	
	private void o() {
		if (StringUtils.isEmpty(start) || StringUtils.isEmpty(end))
			return;
		if (pattern == null)
			return;
		if (pattern.charAt(0) != '^')
			pattern = "^" + pattern;
		if (pattern.charAt(pattern.length() - 1) != '$')
			pattern += "$";
	}
	
	public void search(CharSequence content, Listener listener) {
		if (content == null || listener == null)
			return;
		int pos = 0;
		Pattern p = Pattern.compile(pattern, flags);
		if (StringUtils.isEmpty(start) || StringUtils.isEmpty(end)) {
			Matcher m = p.matcher(content);
			while (m.find(pos)) {
				if (!listener.found(this, m.group(group)))
					break;
				pos = m.end(0);
			}
		} else {
			String c = content.toString();
			pos = c.indexOf(start);
			while (pos != -1) {
				int e = c.indexOf(end, pos + start.length() + 1);
				if (e == -1)
					break;
				String str = c.substring(pos + start.length(), e);
				Matcher m = p.matcher(str);
				if (m.find()) {
					if (!listener.found(this, m.group(group)))
						break;
					pos = c.indexOf(start, e + end.length());
				} else {
					pos = c.indexOf(start, pos + start.length());
				}
			}
		}
	}
	
//	public void replace(String content) {
//		
//	}
	
	public interface Listener {
		public boolean found(Regexp regexp, String str);
	}
}
